package com.ruoyi.project.experiment.experimentteacher.controller;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.experiment.experimentstudent.domian.StudentExperiment;
import com.ruoyi.project.experiment.experimentstudent.service.ITbExperimentReportService;
import com.ruoyi.project.experiment.experimentteacher.domain.TbTeachArrange;
import com.ruoyi.project.experiment.experimentteacher.service.TbTeacherArrangeService;
import com.ruoyi.project.experiment.onlineexperiment.domain.TbExperiment;
import com.ruoyi.project.experiment.onlineexperiment.mapper.TbExperimentMapper;
import com.ruoyi.project.experiment.onlineexperiment.service.ITbExperimentService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.mapper.UserMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TbTeacherArrangeController
 * @Description TODO
 * @Author lvyongwei
 * @Date
 **/
@Controller
@RequestMapping("/system/arrange")
public class TbTeacherArrangeController extends BaseController {

    private String prefix = "system/teacherArrange";

    @Autowired
    private TbExperimentMapper tbExperimentMapper;

    @Autowired
    private ITbExperimentService tbExperimentService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TbTeacherArrangeService tbTeacherArrangeService;

    @Autowired
    private ITbExperimentReportService iTbExperimentReportService;

    @RequiresPermissions("system:arrange:view")
    @GetMapping()
    public String arrange()
    {
        return prefix + "/arrange";
    }

    /**
     * 查询教师布置实验列表
     */
    @RequiresPermissions("system:arrange:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbExperiment tbExperiment)
    {
        startPage();
        List<TbExperiment> list = tbExperimentMapper.getExperiments(tbExperiment);
        return getDataTable(list);
    }


    /**
     * 教师布置实验给全部学生
     */
    @GetMapping("/arrangeAll")
    public String edit(Long experimentId,ModelMap mmap)
    {
        TbExperiment tbExperiment = tbExperimentService.selectTbExperimentById(experimentId);
        mmap.put("experiment",tbExperiment);
        return prefix + "/edit2";
    }

    /**
     * 教师布置实验给全部学生
     * @param tbTeachArrange 实验信息
     * @return
     */
    @RequiresPermissions("system:arrange:edit")
    @Log(title = "教师布置实验给全部学生", businessType = BusinessType.UPDATE)
    @PostMapping("/arrangeAllStudents")
    @ResponseBody
    public AjaxResult arrangeStudents(TbTeachArrange tbTeachArrange)
    {
        String uuid = UUID.randomUUID().toString();
        tbTeachArrange.setTeacherId(getSysUser().getUserId());
        List<User> list = userMapper.getAllStudent();
        /**
         * 设定一个存放登录名
         */
        List<String> ids = new ArrayList<>();
        for(User u:list){
            ids.add(u.getLoginName());
        }
        return toAjax(tbTeacherArrangeService.arrange(tbTeachArrange,ids));
    }


    /**
     * 查看所有实验以及学生提交次数
     */
    @GetMapping("/edit/{experimentId}")
    public String check(@PathVariable("experimentId") Long experimentId,ModelMap modelMap)
    {
        modelMap.put("experimentId",experimentId);
        return prefix + "/result";
    }


    /**
     * 查询学生做此实验情况
     */
    @RequiresPermissions("system:arrange:list")
    @PostMapping("/list2")
    @ResponseBody
    public TableDataInfo result(StudentExperiment studentExperiment)
    {
        startPage();
        List<StudentExperiment> list =  iTbExperimentReportService.getResults(studentExperiment);
        return getDataTable(list);
    }

    /**
     * 修改在线实验项目
     */
    @GetMapping("/edit2/{experimentId}")
    public String check(@PathVariable("experimentId") String experimentId,ModelMap modelMap)
    {
        String[] strArr = experimentId.split(",");
        modelMap.put("reportExperimentId",strArr[0]);
        modelMap.put("reportStuId",strArr[1]);
        /**
         * 跳转到实验，用户多次实验下
         */
        return prefix + "/resultExperiment";
    }

    /**
     * 修改在线实验项目
     */
    /**
     * 查询学生做此实验情况
     */
    @RequiresPermissions("system:arrange:list")
    @PostMapping("/list3")
    @ResponseBody
    public TableDataInfo checkExperiment(StudentExperiment studentExperiment)
    {
        startPage();
        List<StudentExperiment> list =  iTbExperimentReportService.getResultExperiment(studentExperiment);
        return getDataTable(list);
    }



    @GetMapping("/checkScore")
    public String checkExperiment(Long reportExperimentId,Long reportStuId,String uuid,ModelMap modelMap)
    {
        StudentExperiment studentExperiment = new StudentExperiment(reportExperimentId,reportStuId,uuid);
        List<StudentExperiment> studentExperiments = iTbExperimentReportService.getResultExperiment(studentExperiment);
        modelMap.put("report",studentExperiments.get(0));
        return prefix + "/checkScore";
    }


    @GetMapping("/lookScore")
    public String lookScore(Long reportExperimentId,Long reportStuId,String uuid,ModelMap modelMap)
    {
        StudentExperiment studentExperiment = new StudentExperiment(reportExperimentId,reportStuId,uuid);
        List<StudentExperiment> studentExperiments = iTbExperimentReportService.getResultExperiment(studentExperiment);
        modelMap.put("report",studentExperiments.get(0));
        return prefix + "/lookScore";
    }


    /**
     * 教师批改实验，评语+分数
     * @param studentExperiment
     * @return
     */
    @PostMapping("/checkScore2")
    @ResponseBody
    public AjaxResult checkScore2(StudentExperiment studentExperiment) {
        try{
            iTbExperimentReportService.checkScore(studentExperiment);
            return AjaxResult.success("批改成功");
        }catch (Exception e){
            return AjaxResult.error("批改失败");
        }
    }
}
